import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) {
        try {
            // 创建ServerSocket对象
            ServerSocket serverSocket = new ServerSocket(8888);

            // 循环监听客户端连接请求
            while (true) {
                //System.out.println("\nWaiting for client connection...");
                Socket socket = serverSocket.accept();
                //System.out.println( socket.getInetAddress() + ":" + socket.getPort());

                // 获取输入流和输出流
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

                // 读取客户端传来的数据并解析
                String inputLine;
                double var1 = 0.0;
                double var2 = 0.0;
                while ((inputLine = in.readLine()) != null) {
                    //System.out.println("Received from client: " + inputLine);
                    String[] values = inputLine.split(",");
                    if (values.length == 2) {
                        var1 = Double.parseDouble(values[0]);
                        var2 = Double.parseDouble(values[1]);
                        System.out.println("longitude = " + var1);
                        System.out.println("latitude = " + var2);
                        System.out.println("");
                    } else {
                        System.out.println("Invalid input received from client.");
                    }
                    out.println("Server received: " + inputLine);
                }

                // 关闭输入流、输出流和Socket对象
                in.close();
                out.close();
                socket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}